package com.icinfo.cloud.provider.common.utils.fadu;


import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class HttpClientUtil {


    private static final String APPLICATION_JSON = "application/json";
    private static final String CONTENT_TYPE_TEXT_JSON = "text/json";
    private static final String DEFAULT_OF_CHARSET = "UTF-8";

    public static  String postForm(String url, Map<String, String> params) {

        HttpClient httpClient = null;
        String body = "";
        try{
            httpClient = new SSLClient();
            // httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwToken);
            String json = JSON.toJSONString(params);
            HttpPost httpost = new HttpPost(url);
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();

            Set<String> keySet = params.keySet();
            for (String key : keySet) {
                nvps.add(new BasicNameValuePair(key, params.get(key)));
            }

            httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
            body = invoke(httpClient, httpost);
        } catch (Exception ex){
            ex.printStackTrace();
        }
        return body;
    }

    public static String post(String url, Map<String, Object> params, Map<String,String> hearder) throws Exception {
        HttpClient httpClient = null;
        String body = null;
        try{
            httpClient = new SSLClient();
            // httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwToken);
            String json = JSON.toJSONString(params);
            HttpPost post = postJson(url, json,hearder);
            body = invoke(httpClient, post);
        }
        catch (Exception ex){
            ex.printStackTrace();
        }
        return body;
    }
    public static String post(String url, Map<String, String> params) throws Exception {
        HttpClient httpClient = null;
        String body = null;
        try{
            httpClient = new SSLClient();
            String json = JSON.toJSONString(params);
            HttpPost post = postJson(url, json);
            body = invoke(httpClient, post);
        }catch(Exception ex){
            ex.printStackTrace();
            throw ex;
        }
        return body;
    }



    private static String invoke(HttpClient httpclient, HttpUriRequest httpost) throws IOException {
        HttpResponse response = sendRequest(httpclient, httpost);
        String body = paseResponse(response);
        return body;
    }

    private static String paseResponse(HttpResponse response) throws IOException {
        HttpEntity entity = response.getEntity();
        return EntityUtils.toString(entity);
    }

    private static HttpResponse sendRequest(HttpClient httpclient, HttpUriRequest httpost) {
        HttpResponse response = null;
        try {
            response = httpclient.execute(httpost);
            if(response.getStatusLine().getStatusCode() == 200){
                return response;
            }else {
                httpost.abort();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{

        }
        return null;
    }

    private static HttpPost postJson(String url, String json){
        try {
            //String encoderJson = URLEncoder.encode(json, HTTP.UTF_8);
            HttpPost httpPost = new HttpPost(url);
            httpPost.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);
            StringEntity se = new StringEntity(json, DEFAULT_OF_CHARSET);
            se.setContentType(CONTENT_TYPE_TEXT_JSON);
            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON));
            httpPost.setEntity(se);
            return httpPost;
        } catch (Exception e) {
            throw e;
        }
    }

    private static HttpPost postJson(String url, String json, Map<String, String> header){
        try {
            //String encoderJson = URLEncoder.encode(json, HTTP.UTF_8);
            HttpPost httpPost = new HttpPost(url);
            httpPost.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);
            if(header.size()>0){
                Set<String> keys = header.keySet();
                for(String key:keys){
                    httpPost.addHeader(key, header.get(key));
                }
            }

            StringEntity se = new StringEntity(json, DEFAULT_OF_CHARSET);
            se.setContentType(CONTENT_TYPE_TEXT_JSON);
            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON));
            httpPost.setEntity(se);
            return httpPost;
        } catch (Exception e) {
            throw  e;
        }
    }




}
